package com.zhu.transaction.service.imp;

import com.zhu.transaction.mapper.CategoryMapper;
import com.zhu.transaction.mapper.imp.CategoryMapperImp;
import com.zhu.transaction.pojo.BackCategory;
import com.zhu.transaction.pojo.Category;
import com.zhu.transaction.service.CategoryService;
import com.zhu.transaction.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Zhu
 * @createtime 2021/1/21-9:09
 */
@Service
public class CategoryServiceImp implements CategoryService {

    @Autowired
    CategoryMapperImp categoryMapper;

    /**
     * 添加一个新的分类
     *
     * @param firstCategory  一级分类信息
     * @param secondCategory 二级分类信息
     * @return 执行影响行数
     */
    @Override
    public int addCategory(String firstCategory, String[] secondCategory) {
        int result = 0;
        Category firstCategoryPojo = categoryMapper.queryFirstCategory(firstCategory);
        //数据库中没有一级分类,先添加
        if (firstCategoryPojo == null) {
            firstCategoryPojo = new Category();
            firstCategoryPojo.setId(MyUtils.getRandomID()).setCategoryName(firstCategory).setParent("0")
                    .setAddTime(new Date()).setUpdateTime(new Date());
            result += categoryMapper.addCategory(firstCategoryPojo);
        }

        //一次添加二级分类
        for (String categoryName : secondCategory) {
            Category tmpCategory = new Category();
            tmpCategory.setId(MyUtils.getRandomID()).setCategoryName(categoryName)
                    .setParent(firstCategoryPojo.getId()).setAddTime(new Date()).setUpdateTime(new Date());
            result += categoryMapper.addCategory(tmpCategory);
        }
        return result;
    }

    /**
     * 删除订单
     *
     * @param allID 订单Id序列
     * @return
     */
    @Override
    public int deleteCategory(String allID) {
        String[] ids = allID.split(",");
        int result = 0;
        for (String id : ids) {
            result += categoryMapper.deleteCategoryById(id);
        }
        return result == ids.length ? 1 : -1;
    }

    /**
     * 获取所有分类信息
     *
     * @return 分类列表
     */
    @Override
    public List<BackCategory> getAllCategory() {
        List<BackCategory> data = new ArrayList<>();
        //获取所有一级分类
        List<Category> firstCategories = categoryMapper.queryCategoryByParent("0");
        for (Category firstCategory : firstCategories) {
            BackCategory tmp1 = new BackCategory();
            tmp1.setId(firstCategory.getId()).setTitle(firstCategory.getCategoryName());
            //获取此分类下的二级分类
            List<Category> secondCategories = categoryMapper.queryCategoryByParent(firstCategory.getId());
            List<BackCategory> data2 = new ArrayList<>();
            for (Category secondCategory : secondCategories) {
                BackCategory tmp2 = new BackCategory();
                tmp2.setId(secondCategory.getId()).setTitle(secondCategory.getCategoryName());
                data2.add(tmp2);
            }
            tmp1.setChildren(data2);
            data.add(tmp1);
        }
        return data;
    }

    /**
     * 获取二级分类列表
     *
     * @param parent 二级分类的父Id
     * @return 二级分类列表
     */
    @Override
    public List<BackCategory> getSecondCategory(String parent) {
        List<BackCategory> data = new ArrayList<>();
        for (Category secondCategory : categoryMapper.queryCategoryByParent(parent)) {
            BackCategory tmp = new BackCategory();
            tmp.setId(secondCategory.getId()).setTitle(secondCategory.getCategoryName());
            data.add(tmp);
        }
        return data;
    }

    /**
     * 获取分类详情
     *
     * @param id 分类ID
     * @return 分类实体
     */
    @Override
    public Category getCategoryById(String id) {
        Category category = new Category();
        category.setId("0").setParent("0").setCategoryName("全部");
        return id.equals("0") ? category : categoryMapper.queryCategoryById(id);
    }

    /**
     * 更新分类信息
     *
     * @param id           分类ID
     * @param categoryName 新的分类名
     * @return sql执行影响行数
     */
    @Override
    public int updateCategory(String id, String categoryName) {
        Category category = categoryMapper.queryFirstCategory(categoryName);
        //该一级分类已经存在，不允许进行更新
        if (category != null) {
            return -2;
        }
        return categoryMapper.updateCategory(id, categoryName);
    }
}
